package com.dawn.module.hospital.controller.app.register;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson.JSONObject;
import com.dawn.framework.apisign.core.annotation.Sign;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.web.core.util.RequestUtils;
import com.dawn.module.hospital.controller.admin.statics.vo.RegisterRespVO;
import com.dawn.module.hospital.controller.admin.statics.vo.SearchReqVO;
import com.dawn.module.hospital.controller.app.base.vo.ReqVO;
import com.dawn.module.hospital.controller.app.register.vo.*;
import com.dawn.module.hospital.enums.ErrorCodeConstants;
import com.dawn.module.hospital.service.RegisterService;
import com.dawn.module.hospital.service.StaticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: cnsu-cmh
 * @time: 2024/8/7 13:45
 */
@Tag(name = "挂号 APP - bh")
@RestController
@RequestMapping("/bh/register")
@Validated
@Slf4j
public class AppRegisterController {

    @Autowired
    private RegisterService registerService;

    @Autowired
    private StaticsService staticsService;

    @Sign
    @PostMapping(value = "/date")
    @Operation(summary = "获取挂号日期")
    public CommonResult dateList(@Valid @RequestBody ReqVO reqVO){
        return registerService.dateList(reqVO);
    }

    @Sign
    @Operation(summary = "获取挂号科室")
    @PostMapping(value = "/dept")
    public CommonResult deptList(@Valid @RequestBody RegisterDeptQueryReqVO reqVO){
        return registerService.deptList(reqVO);
    }

    @Sign
    @Operation(summary = "获取挂号医生")
    @PostMapping(value = "/doctor")
    public CommonResult doctorList(@Valid @RequestBody RegisterDoctorQueryReqVO reqVO){
        return registerService.doctorList(reqVO);
    }

    @Operation(summary = "号源检查")
    @PostMapping(value = "/source/check")
    public CommonResult sourceCheck(@Valid @RequestBody RegisterSourceCheckReqVO reqVO) {
        return registerService.sourceCheck(reqVO);
    }

//    @Sign
    @Operation(summary = "创建挂号信息")
    @PostMapping(value = "/create")
    public CommonResult createOrder(@Valid @RequestBody RegisterCreateReqVO reqVO) {
        return registerService.register(reqVO);
    }

    @Operation(summary = "预约免费号")
    @PostMapping(value = "/free")
    public CommonResult freeOrder(@RequestBody HashMap reqVO) {
        String orderNumber = String.valueOf(reqVO.get("orderNumber"));
        CommonResult result = registerService.queryByOrderNumber(orderNumber);
        if(result.isError()){
            return result;
        }
        Map map = (Map) result.getData();

        int amount = new BigDecimal(String.valueOf(map.get("amount"))).multiply(new BigDecimal(100)).intValue();

        if(amount > 0){
            return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"预约免费号失败");
        }
        return registerService.payNotify(reqVO);
    }

    @PostMapping(value = "/dopay")
    @Operation(summary = "挂号缴费下单")
    public CommonResult dopay(@Valid @RequestBody RegisterOrderQueryReqVO reqVO) {
        return registerService.buildPaySignData(reqVO.getOrderNumber());
    }

    @Sign
    @Operation(summary = "查询挂号订单信息")
    @PostMapping(value = "/query/order")
    public CommonResult queryOrder(@Valid @RequestBody RegisterOrderQueryReqVO reqVO) {
        return registerService.queryByOrderNumber(reqVO.getOrderNumber());
    }

    @Sign
    @Operation(summary = "挂号记录")
    @PostMapping("/list")
    public CommonResult<PageResult<RegisterRespVO>> pageOutpatient(@Valid @RequestBody SearchReqVO reqVO){
        if (reqVO.getSearchTime() != null && reqVO.getSearchTime().length > 1) {
            LocalDateTime beginDateTime = reqVO.getSearchTime()[0];
            LocalDateTime endDateTime = reqVO.getSearchTime()[1];
            reqVO.setBeginDate(LocalDateTimeUtil.formatNormal(beginDateTime));
            reqVO.setEndDate(LocalDateTimeUtil.formatNormal(endDateTime));
        }

        PageResult<RegisterRespVO> pageResult = staticsService.getRegisterPage(reqVO);

        return CommonResult.success(pageResult);
    }

    @Sign
    @Operation(summary = "退号退费")
    @PostMapping(value = "/cancel")
    public CommonResult regCancel(@Valid @RequestBody RegisterOrderQueryReqVO reqVO) {
        return registerService.registerCancel(reqVO.getOrderNumber());
    }

    @Sign
    @Operation(summary = "解锁号源")
    @PostMapping(value = "/un-lock")
    public CommonResult unLockRegNum(@Valid @RequestBody RegisterQueryReqVO reqVO) {
        return registerService.unLockRegNum(reqVO.getOrderNumber());
    }


    @Operation(summary = "挂号缴费成功通知")
    @RequestMapping(value = "/notify")
    public CommonResult payNotify(HttpServletRequest request){
        JSONObject params = RequestUtils.getReqParamJSON(request);
        CommonResult result = registerService.payNotify(params);
        return result;
    }

    @Operation(summary = "获取就诊记录")
    @PostMapping(value = "/see-record")
    public CommonResult seeRecord(@Valid @RequestBody RegisterQueryReqVO reqVO) {
        return registerService.seeRecord(reqVO);
    }

    @Operation(summary = "获取候诊信息")
    @PostMapping(value = "/wait-see")
    public CommonResult waitSee(@Valid @RequestBody RegisterQueryReqVO reqVO) {
        return registerService.waitSee(reqVO);
    }


    @Sign
    @Operation(summary = "就诊签到")
    @PostMapping(value = "/sign-in")
    public CommonResult signIn(@Valid @RequestBody RegisterQueryReqVO reqVO){
        return registerService.signIn(reqVO);
    }
}
